package com.senac.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.senac.model.Professor;
import com.senac.parser.Parser;
import com.senac.parser.ProfessorParser;


public class ProfessorDao {

	
	public List<Professor> getProfessores() {		
		Connection con =  null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Professor> list = new ArrayList<>();
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("SELECT * FROM PROFESSOR");
			rs = ps.executeQuery();
			Parser<Professor> pParser = new ProfessorParser(rs);
			while(rs.next()){
				list.add(pParser.parse());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(rs, ps, con);
		}				
		return list;
	}
	
	public Professor getProfessorById(Integer id) {
		Connection con =  null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Professor professor = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("SELECT * FROM PROFESSOR WHERE ID = ?");
			ps.setInt(1, id);
			rs = ps.executeQuery();
			Parser<Professor> pParser = new ProfessorParser(rs);
			if(rs.next()) {
				professor = pParser.parse();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(rs, ps, con);
		}
		return professor;
	}

	public boolean save(Professor professor) {
		Connection con =  null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("INSERT INTO PROFESSOR (nome, carga_horaria, titulacao) VALUES (?, ?, ?)");			
			ps.setString(1, professor.getNome());
			ps.setInt(2, professor.getCargaHoraria());
			ps.setInt(3, professor.getTitulacao().ordinal());
			return ps.executeUpdate() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}				
	}

	public boolean update(Professor professor) {
		Connection con =  null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("UPDATE PROFESSOR SET nome = ?, carga_horaria = ?, titulacao = ? WHERE ID = ?");			
			ps.setString(1, professor.getNome());
			ps.setInt(2, professor.getCargaHoraria());
			ps.setInt(3, professor.getTitulacao().ordinal());
			ps.setInt(4, professor.getId());
			return ps.executeUpdate() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}	}

	public boolean delete(Integer id) {
		Connection con =  null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("DELETE FROM PROFESSOR WHERE ID = ?");			
			ps.setInt(1, id);
			return ps.executeUpdate() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}
	}
	
	public int getCargaHorariaTotal(int id, int idDisciplina) {
		Connection con =  null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int cargaHoraria = 0;
		try {
			con = ConnectionFactory.getConnection();
			String query = "SELECT SUM( carga_horaria ) AS total FROM  DISCIPLINA WHERE id_professor = ? AND id != ?";
			ps = con.prepareStatement(query);
			ps.setInt(1, id);
			ps.setInt(2, idDisciplina);
			
			rs = ps.executeQuery();
			if(rs.next()) {
				cargaHoraria = rs.getInt("total");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(rs, ps, con);
		}
		return cargaHoraria;
	}

	
}
